/***********************************************************
 *  File_Name  : XJINIProfile.h
 *  File_Path   : 
 *  File_Encode : UTF8
 *  Description : INI配置文件读写类
 *  
 *  OS : Linux, UNIX,Windows,Other
 *  LastVersion  : 20130603
 *      
 *  Author      : LiXiujie(李秀杰)
 *  Date        : 2013-6-3
 *  Version     : 20130603
 *  Description : 
 *                
 *  Edit Author : 
 *  Edit Date   : 
 *  Version     : 
 *  Description :
 *                
 ***********************************************************/

#ifndef XJINIPROFILE_H_
#define XJINIPROFILE_H_


#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <cerrno>
using namespace std;

namespace XJUtilClass {


class XJINIProfileDataItem{
public:
	XJINIProfileDataItem(){
		m_strCate = "Default";
		m_strName = "Default";
		m_strDesc = "";
		m_strValue = "";
		m_nDescType = 0;
	}
	XJINIProfileDataItem(const string &strItemName, const string & strValue){
		m_strCate = "Default";
		m_strName = strItemName;
		m_strDesc = "";
		m_strValue = strValue;
		m_nDescType = 0;
	}
	XJINIProfileDataItem(const string & strCategoryName, const string & strItemName, const string & strValue){
		m_strCate = strCategoryName;
		m_strName = strItemName;
		m_strValue = strValue;
		m_nDescType = 0;
	}
	XJINIProfileDataItem(const string & strCategoryName, const string & strItemName, const string & strItemDesc, const string & strValue){
		m_strCate = strCategoryName;
		m_strName = strItemName;
		m_strDesc = strItemDesc;
		m_strValue = strValue;
		m_nDescType = 0;
	}
	XJINIProfileDataItem(const XJINIProfileDataItem &item){
		m_strCate = item.m_strCate;
		m_strName = item.m_strName;
		m_strDesc = item.m_strDesc;
		m_strValue = item.m_strValue;
		m_nDescType = item.m_nDescType;
	}
	XJINIProfileDataItem& operator = (const XJINIProfileDataItem &item){
		if (this == &item){
			return *this;
		}
		m_strCate = item.m_strCate;
		m_strName = item.m_strName;
		m_strDesc = item.m_strDesc;
		m_strValue = item.m_strValue;
		m_nDescType = item.m_nDescType;
		return *this;
	}
	const string &GetCate(){
		return m_strCate;
	}
	const string &GetName(){
		return m_strName;
	}
	const string &GetDesc(){
		return m_strDesc;
	}
	const string &GetValue(){
		return m_strValue;
	}

	XJINIProfileDataItem& Set(const string & strItemName, const string & strValue){
		m_strName = strItemName;
		m_strValue = strValue;
		return *this;
	}
	XJINIProfileDataItem& Set(const string & strCategoryName, const string & strItemName, const string & strValue){
		m_strCate = strCategoryName;
		m_strName = strItemName;
		m_strValue = strValue;
		return *this;
	}
	XJINIProfileDataItem& SetCate(const string & strSrc){
		m_strCate = strSrc;
		return *this;
	}
	XJINIProfileDataItem& SetName(const string & strSrc){
		m_strName = strSrc;
		return *this;
	}
	XJINIProfileDataItem& SetDesc(const string & strSrc){
		m_strDesc = strSrc;
		return *this;
	}
	XJINIProfileDataItem& SetValue(const string & strSrc){
		m_strValue = strSrc;
		return *this;
	}
public:
	string m_strCate;
	string m_strName;
	string m_strDesc;
	string m_strValue;
	int    m_nDescType; // 描述显示方式，0 上, 1 右
};

class XJINIProfile {
public:
	XJINIProfile(bool bAutoTrimSpace = true);
	XJINIProfile(const string &strFilePath, bool bAutoTrimSpace = true);
	virtual ~XJINIProfile();

	int AddItem(const XJINIProfileDataItem &item);
	int SetItem(const XJINIProfileDataItem &item);
	int DelItem(const string & strItemName);
	int DelItem(const string & strCategoryName, const string & strItemName);
	XJINIProfileDataItem & GetItem(const string & strItemName);
	XJINIProfileDataItem & GetItem(const string & strCategoryName, const string & strItemName);
	string &GetItemValue(const string & strItemName);
	string &GetItemValue(const string & strCategoryName, const string & strItemName);
	int ReadFile(const string &strFilePath);
	int WriteFile(const string &strFilePath);
	string &GetLastError();
	void SetLineEndSymbol(const string &strSymbol = "\n");
	void SetCommentSymbol(const string &strSymbol = ";");
	void Clear();
	void SetAutoTrimSpace(bool bTrim);
	string &TrimHeadTail(string &strSource, const char ch = ' ');

	string &GetV(const string & strItemName);
	string &GetV(const string & strCategoryName, const string & strItemName);
	int GetCategoryNameList(vector<string >& veCategoryName);
	int GetItemNameList(vector<string >& veItemName, const string & strCategoryName);
private:
	vector<XJINIProfileDataItem > m_veDataItems;
	string m_strLastError;
	XJINIProfileDataItem m_itemNull;
	string m_strLineEndSymbol;
	string m_strCommentSymbol;
	bool  m_bTrim;

};

} /* namespace XJUtilClass */
#endif /* XJINIPROFILE_H_ */
